<template>
  <div class="full-height bg-white page-content content">
    <collapse ref="collapse" class="collapse-content after">
      <template v-slot:content>
        <yz-condition-filter
          v-model="queryParams"
          @resetSearch="resetSearch"
          @onSubmit="onSubmit"
        >
        <select-tooltip
            v-model="queryParams.workshopId"
            :multiple="false"
            @input="onSelectWorkShop"
            isShowAll
            :options="workshopslist"
            msg="所在车间"
          ></select-tooltip>
        <select-tooltip
            v-model="queryParams.deviceType"
            :multiple="false"
            isShowAll
            @input="onSelectDeviceType"
            :options="deviceTypesList"
            msg="设备类型"
          ></select-tooltip>
            <fuzzy-search
              v-model.trim="queryParams.deviceCode"
              :fuzzyParams="fuzzyParams"
              fuzzyValue="assetNumber"
              :fuzzyObj="['name', 'id']"
              isFilter
              class="margin-r-20"
              isShowAll
              title="加工设备"
              :fuzzyFunc="fuzzyFunc"
            ></fuzzy-search>
        <yz-input
              v-model.trim="queryParams.dateRange"
              class="q-mr-md"
              inputType="date"
              hint="请选择日期范围"
              isTip
              title="派单日期"
         ></yz-input>
        <select-tooltip
            v-model="queryParams.maintenanceStatus"
            :multiple="false"
            isShowAll
            :options="maintenanceStatusOptions"
            msg="保养状态"
          ></select-tooltip>
          <select-tooltip
            v-model="queryParams.maintenanceCycle"
            :multiple="false"
            isShowAll
            :options="maintenanceCycleOptions"
            msg="保养周期"
          ></select-tooltip>
        </yz-condition-filter>
          <div class="form-inline bottom padding-b-20" style="justify-content: flex-end;">
              <select-input ref="selectInput"
                            :list="searchCondition"
                            @search="inputSearch"
                            @clearSearchVal="clearSearchVal" />
          </div>
      </template>
    </collapse>
    <q-table
            :data="tableData"
            :columns="columns"
            row-key="id"
            class="myTable scroll stick-last-col"
            :loading="isTableLoading"
            :pagination.sync="pagination"
            :hide-bottom="tableData.length > 0"
            color="primary"
          >
      <template v-slot:body-cell-maintenanceType="props">
        <q-td>
          {{ $getEnum(Enums.MAINTENANCE_TYPE, props.row.maintenanceType).desc | toHorizontalLine}}
        </q-td>
      </template>
      <template v-slot:body-cell-maintenanceStatus="props">
        <q-td>
          <set-color :options="maintenanceStatusOptions" :colorType="props.row.maintenanceStatus">
          </set-color>
        </q-td>
      </template>
      <template v-slot:body-cell-assignedShiftName="props">
        <q-td>
          {{ props.row.assignedShiftName | toHorizontalLine }}
        </q-td>
      </template>
      <template v-slot:body-cell-sparePartCount="{ row }">
        <q-td>
          <span v-if="!row.sparePartCount || row.sparePartCount === '--'" class="q-ml-md">{{row.sparePartCount | toHorizontalLine}}</span>
          <q-btn flat
                 v-else
                 color="primary"
                 :disable="row.sparePartCount === 0"
                 class="qa-d-text"
                 @click="onChangePart(row)">{{row.sparePartCount}}</q-btn>
        </q-td>
      </template>
      <template v-slot:body-cell-scheduleNo="props">
        <q-td>
          {{ props.row.scheduleNo | toHorizontalLine }}
        </q-td>
      </template>
      <template v-slot:body-cell-operatorNameList="props">
        <q-td>
          {{ props.row.operatorNameList && props.row.operatorNameList.join() | toHorizontalLine }}
        </q-td>
      </template>
      <template v-slot:body-cell-recurRuleFreq="props">
        <q-td>
          {{ maintenanceStatusObj[props.row.recurRuleFreq] | toHorizontalLine }}
        </q-td>
      </template>
      <template v-slot:header-cell-assignedDate="props">
        <q-th :props="props"
              class="sortable sorted-icon"
              @click="onSeletByTime">
            {{props.col.label}}
            <q-icon class="q-table__sort-icon"  :name="queryParams.sort ? 'arrow_upward' : 'iconfont iconjiantoupaixu'"></q-icon>
        </q-th>
      </template>
      <template v-slot:body-cell-operations="props">
        <q-td class="operations" style="width: 56px">
          <div class="btn-group">
            <table-btn btnType="detail" @click="onDetail(props.row)"/>
          </div>
        </q-td>
      </template>
    </q-table>
    <yz-pagination v-model="pagination" @onSearch="getList"/>
    <change-part-dialog :show="isShowPartDialog" @close="isShowPartDialog = false" :partId="partId" type="2"/>
    </div>
</template>
<script>
import Collapse from 'src/components/collapse/index'
import MAINTENANCEMIXIN from './maintenance-mixin'
import { getMaintenaceOrdersList } from 'src/boot/maintenance/maintenance-order'
import SelectInput from 'src/components/select-input/index'
import _ from 'lodash'
import ChangePartDialog from '../spare/parts-list/change-part-dialog'
export default {
  name: 'maintenance-order',
  mixins: [MAINTENANCEMIXIN],
  components: {
    ChangePartDialog,
    Collapse,
    SelectInput
  },
  data () {
    return {
      queryParams: {
        dateRange: [],
        workshopId: 'all',
        sort: 0,
        maintenanceStatus: 'all',
        maintenanceCycle: 'all',
        deviceType: 'all',
        deviceCode: ''
      },
      keyType: '',
      keyWord: '',
      partId: '',
      tableData: [], // 列表数据源
      isShowPartDialog: false,
      isTableLoading: false, // table数据列表loading动画
      columns: [
        { name: 'orderNo', label: '保养单号', align: 'left', field: 'orderNo' },
        { name: 'assetNumber', label: '设备编号', align: 'left', field: 'assetNumber' },
        { name: 'deviceName', label: '设备名称', align: 'left', field: 'deviceName' },
        { name: 'deviceTypeName', label: '设备类型', align: 'left', field: 'deviceTypeName' },
        { name: 'maintenanceType', label: '保养类型', align: 'left', field: 'maintenanceType' },
        { name: 'maintenanceProgramName', label: '保养项目', align: 'left', field: 'maintenanceProgramName' },
        { name: 'maintenanceStatus', label: '保养状态', align: 'left', field: 'maintenanceStatus' },
        { name: 'recurRuleFreq', label: '保养周期', align: 'left', field: 'recurRuleFreq' },
        { name: 'assignedShiftName', label: '班制名称', align: 'left', field: 'assignedShiftName' },
        { name: 'sparePartCount', label: '更换备件', align: 'left', field: 'sparePartCount' },
        { name: 'operatorNameList', label: '保养人', align: 'left', field: 'operatorNameList' },
        { name: 'scheduleNo', label: '计划编号', align: 'left', field: 'scheduleNo' },
        { name: 'assignedDate', label: '派单时间', align: 'left', field: 'assignedDate' },
        {
          name: 'operations',
          field: 'operations',
          label: '操作',
          align: 'center'
        }
      ],
      searchCondition: [
        {
          id: '1',
          name: '保养人'
        },
        {
          id: '2',
          name: '计划编号'
        },
        {
          id: '3',
          name: '保养单号'
        }
      ]
    }
  },
  computed: {
    //  保养周期枚举
    maintenanceCycleOptions () {
      return this.$getEnum(this.Enums.MAINTENANCE_CYCLE)
    },
    //  保养状态枚举
    maintenanceStatusOptions () {
      return this.$getEnum(this.Enums.MAINTENANCE_STATUS)
    },
    maintenanceStatusObj () {
      const statusObj = {}
      this.maintenanceCycleOptions.forEach(item => {
        statusObj[item.value] = item.label
      })
      return statusObj
    }
  },
  beforeRouteEnter (to, from, next) {
    to.name === 'maintenanceOrder' && next(vm => {
      vm.getList(_.isEmpty(to.params))
    })
  },
  watch: {},
  methods: {
    onChangePart (row) {
      console.log(row)
      this.isShowPartDialog = true
      console.log('更换备件')
    },
    // 模糊查询
    inputSearch (item, searchVal) {
      this.pagination.pageNum = 1
      this.keyWord = searchVal
      this.keyType = item.id
      this.getList()
    },
    // 清空搜索值
    clearSearchVal () {
      this.keyWord = ''
      this.keyType = ''
      this.pagination.pageNum = 1
      this.getList()
    },
    onDetail (row) {
      this.$router.push({
        path: '/main/repair/maintenance-order/detail-order',
        query: {
          id: row.id
        }
      })
    },
    // 切换页码
    getList (isQuery = true) {
      if (!isQuery) {
        const member = this.$route.params
        this.$refs.selectInput.setVal(member.orderNo, 2)
      }
      const params = {
        ...this.queryParams,
        ...this.pagination,
        keyWord: this.keyWord,
        keyType: this.keyType
      }
      let [ startDate, endDate ] = this.$utils.setDateOSI(this.queryParams.dateRange, true, 'YYYY-MM-DD')
      params.startDate = startDate
      params.endDate = endDate
      this.isTableLoading = true
      getMaintenaceOrdersList(params).then((res) => {
        this.isTableLoading = false
        const ret = res.data
        this.tableData = ret.list
        this.pagination.rowsNumber = ret.total
      }).catch((err) => {
        this.isTableLoading = false
        console.log(err)
      })
    }
  }
}
</script>
